package com.imooc.security.core.social;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionSignUp;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.social.security.SpringSocialConfigurer;

import com.imooc.security.core.properties.SecurityProperties;
import com.imooc.security.core.social.support.ImoocSpringSocialConfigurer;
import com.imooc.security.core.social.support.SocialAuthenticationFilterPostProcessor;

/**
 * 5.4.8 添加Social配置类
 */
@Configuration
@EnableSocial
public class SocialConfig extends SocialConfigurerAdapter{
	
	@Autowired
	private DataSource dataSource;
	
	@Autowired
	private SecurityProperties securityProperties;

	@Autowired(required = false)
	private ConnectionSignUp connectionSignUp;
	
	@Autowired(required = false)
	private SocialAuthenticationFilterPostProcessor socialAuthenticationFilterPostProcessor;
	
	/**
	 * UsersConnectionRepository包附近
	 * 5.4.9 建表 /org/springframework/social/connect/jdbc/JdbcUsersConnectionRepository.sql
	 */
	
	/**
	 * 5.4.10 获取用户Social持久化对象，并制定加密规则
	 */
	@Override
	public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
		JdbcUsersConnectionRepository repository = new JdbcUsersConnectionRepository(dataSource, connectionFactoryLocator, Encryptors.noOpText());
		repository.setTablePrefix("imooc_");
		
		// 5.6.16 使第一次社交登陆时，默默注册一个用户，而不跳转到注册页
		// imooc_userconnection会使用displayName作为Id插一条记录
		if(connectionSignUp!=null){
			repository.setConnectionSignUp(connectionSignUp);
		}
		return repository;
	}
	
	/**
	 * 5.4.19 声明SocialSecurityConfigurer Bean
	 */
	@Bean
	public SpringSocialConfigurer imoocSocialSecurityConfig(){
		// 5.5.3 重构，使用自己的filterProcessesUrl配置
		// return new SpringSocialConfigurer();
		String filterProcessesUrl = securityProperties.getSocial().getFilterProcessesUrl();
		ImoocSpringSocialConfigurer configurer = new ImoocSpringSocialConfigurer(filterProcessesUrl);
		
		// 5.6.7 社交首次登录，注册跳转到自定义signUpUrl
		configurer.signupUrl(securityProperties.getBrowser().getSignUpUrl());
		
		// 6.6.9 授权码模式-浏览器环境下用session；app环境下初始化socialSecurityConfigurer的时候将PostProcessor注入进来用redis 6.7.6
		configurer.setSocialAuthenticationFilterPostProcessor(socialAuthenticationFilterPostProcessor);
		
		return configurer;
	}
	
	/**
	 * 5.6.8 声明社交登录注册工具类
	 */
	@Bean
	public ProviderSignInUtils providerSignInUtils(ConnectionFactoryLocator connectionFactoryLocator){
		return new ProviderSignInUtils(connectionFactoryLocator,getUsersConnectionRepository(connectionFactoryLocator));
	}
}
